package com.ywwxhz.service;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Build;
import android.os.IBinder;
import android.provider.Settings;

import com.ywwxhz.Util.Config;
import com.ywwxhz.Util.PreferencesKit;
import com.ywwxhz.Util.ToolKit;
import com.ywwxhz.plugins.battery.BatteryModel;
import com.ywwxhz.plugins.eyecare.EyesCareModel;
import com.ywwxhz.plugins.tasks.TaskModel;

public class CoreService extends Service {
    private static final String TAG = "CoreService";
    private Context context;
    private TaskModel taskModel = null;
    private BatteryModel batteryModel = null;
    private EyesCareModel eyesCareModel = null;

    @Override
    public void onCreate() {
        super.onCreate();
        ToolKit.Log(TAG, "onCreate");
        context = getApplicationContext();
        taskModel = TaskModel.getInstance(context);
        batteryModel = BatteryModel.getInstance(context);
        eyesCareModel = EyesCareModel.getInstance(context);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @SuppressWarnings("deprecation")
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent != null) {
            ToolKit.Log(TAG, "" + intent.getExtras().toString());
            switch (intent.getIntExtra(Config.SERVICE_PARAM, 0)) {
                case TaskModel.MODEL:
                    taskModel.execute(intent);
                    break;
                case BatteryModel.MODEL:
                    batteryModel.execute(intent);
                    break;
                case EyesCareModel.MODEL:
                    eyesCareModel.execute(intent);
                    break;
                default:
                    taskModel.onRecover(context);
                    batteryModel.onRecover(context);
                    eyesCareModel.onRecover(context);
                    break;
            }
        } else {
            ToolKit.Log(TAG, "intent = null call recovery");
            taskModel.onRecover(context);
            batteryModel.onRecover(context);
            eyesCareModel.onRecover(context);
        }
        Intent pintent = new Intent(
                Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        Uri uri = Uri.fromParts("package", getPackageName(), null);
        pintent.setData(uri);
        Notification notification = new Notification.Builder(this)
                .setContentIntent(
                        PendingIntent.getActivity(this, 0, pintent, 0))
                .setWhen(System.currentTimeMillis()).getNotification();
        int nid = 1;
        if (PreferencesKit.getboolean(getApplicationContext(), "hideNotify", false)) {
            if (Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1 < Build.VERSION.SDK_INT) {
                nid = 0;
            }
        }
        startForeground(nid, notification); // 1为notification id
        System.gc();
        ToolKit.Log(TAG, "taskModel: " + taskModel.getStatus() + " batteryModel: "
                + batteryModel.getStatus() + " eyesCareModel: " + eyesCareModel.getStatus());
        if (!taskModel.getStatus() && !batteryModel.getStatus() && !eyesCareModel.getStatus()) {
            ToolKit.Log(TAG, "all model off call stopSelf()");
            stopSelf();
        }
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        ToolKit.Log(TAG, "onDestroy()");
        taskModel.onDestroy();
        batteryModel.onDestroy();
        eyesCareModel.onDestroy();
        stopForeground(true);
        super.onDestroy();
        ToolKit.Log(TAG, "CoreServer Termated");
        System.exit(0);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        ToolKit.Log(TAG, "onConfigurationChanged()");
        taskModel.onConfigChange(newConfig);
        batteryModel.onConfigChange(newConfig);
        eyesCareModel.onConfigChange(newConfig);
        super.onConfigurationChanged(newConfig);
    }

}
